const MsgCode = require('./../../../define/MsgCode');
const MAP_ID = require('./../../../define/MAP_ID');

module.exports = function (app) {
    return new Handler(app);
};

let Handler = function (app) {
    this.app = app;
};

/**
 * 游戏通知处理
 * @param {Object} msg
 * @param {Object} sid
 * @param {Function} next
 * */
Handler.prototype.onNotify = function (msg, sid, next) {
    let code = msg['code'];
    let data = msg['data'];
    if (data.hasOwnProperty('room_id')) {
        let uid = msg['data']['uid'];
        let room_id = msg['data']['room_id'];
        if (this.app.id2Room.hasOwnProperty(room_id)) {
            let room = this.app.id2Room[room_id];
            switch (code) {
                case MsgCode.LeaveRoom:
                    room.removePlayer(uid);
                    break;
                default:
                    room.onNotify(msg, sid);// 这里交给房间自己处理
                    break;
            }
        } else {
            console.error('没有该房间【' + room_id + '】');
        }
    } else {
        console.error('消息结构中没有【room_id】字段');
    }
    next();
};